package com.bRoom.controller.front;

import com.bRoom.common.Conts;
import com.bRoom.common.ServerResponse;
import com.bRoom.pojo.User;
import com.bRoom.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("front/user")
public class IUserController {

    @Autowired
    private IUserService iUserService;

    @RequestMapping("login.do")
    @ResponseBody
    public ServerResponse<User> login(String userName, String userPassword, HttpSession session)
    {

        ServerResponse<User> response=iUserService.login(userName,userPassword);
        if (response.isSuccess()){
            session.setAttribute(com.bRoom.common.Conts.CURRENT_USER,response.getData());
        }
        return  response;
    }

    @RequestMapping("register.do")
    @ResponseBody
    public  ServerResponse<User> register(User user){
          return iUserService.register(user);
    }

    @RequestMapping("logout.do")
    @ResponseBody
    public ServerResponse logout(HttpSession session){

          session.removeAttribute(Conts.CURRENT_USER);
          return ServerResponse.createBySuccessMsg("注销成功");
    }

    @RequestMapping("update_message.do")
    @ResponseBody
    public ServerResponse<User> updateMessage(User user,HttpSession session){
          User currentUser=(User) session.getAttribute(Conts.CURRENT_USER);
          if (currentUser==null){
               return ServerResponse.createByErrorMsg("用户未登录，请先登录");
          }
              return iUserService.updateMessage(user,currentUser.getUserId());
    }


    @RequestMapping("find_question.do")
    @ResponseBody
    public ServerResponse findQuestion(String userName){
            return iUserService.findQuestion(userName);
    }

    @RequestMapping("find_answer.do")
    @ResponseBody
    public  ServerResponse findAnswer(String userName,String question,String answer){
      return iUserService.findAnswer(userName,question,answer);
    }

    @RequestMapping("forget_password_resert.do")
    @ResponseBody
    public ServerResponse forgetPasswordResert(String userName,String passwordNew,String forgetToken){
        return iUserService.forgetPasswordResert(userName,passwordNew,forgetToken);
    }

    @RequestMapping("login_resert_password.do")
    @ResponseBody
    public  ServerResponse loginResertPassword(String passWordOld,String passWordNew,HttpSession session){
          User user=(User) session.getAttribute(Conts.CURRENT_USER);
          if (user==null){
               return ServerResponse.createByErrorMsg("用户未登录，请先登录");
          }
          return iUserService.loginResertPassword(passWordOld,passWordNew,user.getUserId());
    }
}
